/* ****************************************************************************
 *
 * Copyright (c) Microsoft Corporation. 
 *
 * This source code is subject to terms and conditions of the Microsoft Public License. A 
 * copy of the license can be found in the License.html file at the root of this distribution. If 
 * you cannot locate the  Microsoft Public License, please send an email to 
 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 *
 * You must not remove this notice, or any other, from this software.
 *
 * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
 * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
 * PARTICULAR PURPOSE.
 * ***************************************************************************/

#include "stdafx.h"
#include <pgl/core/pgl.h>
#include <pgl/core/PGLLineHor.h>
#include <pgl/core/PGLRegion.h>

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif


PGL_IMPLEMENT_DYNCREATE(CPGLLineHor, CPGLLine);

//! Save settings
void CPGLLineHor::SerializeXML(CPGLArchive& ar)
{
	static const TCHAR szTag[] = _T("CPGLLineHor");

	markup::CMarkupArchive& xml = *((markup::CMarkupArchive*)ar.GetMarkup());
	if(ar.IsStoring())
	{
		xml.AddChildElem(szTag);
		xml.AddChildAttrib(_T("Name"),GetName());
		xml.IntoElem();

			/////////////////////////////////////////////////////////
			// Adding base class
			CPGLLine::SerializeXML(ar);

			xml.AddChildElemEx(_T("Y"),m_dY);
			xml.AddChildElem(_T("Caption"));
			xml.IntoElem();
				m_tCaption.SerializeXML(ar);
			xml.OutOfElem();

		xml.OutOfElem();
	}
	else
	{
		if  (!xml.FindChildElem(szTag))
				return;

		xml.IntoElem();

			/////////////////////////////////////////////////////////
			// loading base class
			CPGLLine::SerializeXML(ar);

			if (xml.FindChildElem(_T("Y")))
				m_dY=xml.GetChildDataDouble();

			xml.ResetChildPos();
			if(xml.FindChildElem(_T("Caption")))
			{
				xml.IntoElem();
					m_tCaption.SerializeXML(ar);
				xml.OutOfElem();
			}

		xml.OutOfElem();

		NeedUpdateExtent();
	}
}


#ifdef _DEBUG
void CPGLLineHor::Dump( CDumpContext& dc ) const
{
    // call base class function first
    CPGLLine::Dump( dc );
}
void CPGLLineHor::AssertValid() const
{
    // call inherited AssertValid first
    CPGLLine::AssertValid();

} 
#endif


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CPGLLineHor::CPGLLineHor()
: CPGLLine()
{
	m_dY = 0;
	m_tCaption.SetColor(0,0,0);
}

CPGLLineHor& CPGLLineHor::operator =(const CPGLLineHor& lv)
{
	if (this != &lv)
	{
		this->CPGLLineHor::operator =(lv);
		m_dY = lv.m_dY;
		m_tCaption = lv.m_tCaption;
	}
	return *this;
}

CPGLLineHor::~CPGLLineHor()
{
}

void CPGLLineHor::GetUnionExtent(gfxinterface::CGfxInterface& gfx,CPGLRectD& rExtent)
{
	rExtent.MakeUnionY(m_dY);

	rExtent.xValid=false;
	rExtent.yValid=true;
}

void CPGLLineHor::PlotGfx(gfxinterface::CGfxInterface& gfx)
{
	using namespace gfxinterface;

	if (!IsVisible())
		return;

	// calling base class plot
	CPGLObject::PlotGfx(gfx);
	
	gfx.AddComment(_T("--- CPGLLineHor ---"));	
	SetDashGfx(gfx);	
	// set line width
	gfx.SetLineWidth(GetLineWidth());	
	gfx.DrawLine(gfx.GetWorldLeft(), m_dY, gfx.GetWorldRight(), m_dY);

	// setting caption position
	CPGLCoord2D& pos=m_tCaption.GetPosition();
	pos.SetPosition( 0, 0, CPGLCoord2D::Coord2DNormClip);
	pos.SetBBTrans(SPointD( 5, GetLineWidth()+5));
	pos.SetWorldTrans(SPointD(0, m_dY));

/*
	float pos[2];
	pos[0]=(float)(gfx.GetWorldClipLeft()+gfx.BBToWorldAbsx(5));
	pos[1]=(float)(m_dY+gfx.BBToWorldAbsy(GetLineWidth()+5));
	m_tCaption.SetPosition(pos);
*/
	m_tCaption.PlotGfx(gfx);
}

void CPGLLineHor::PlotLegendGfx(gfxinterface::CGfxInterface& gfx, const CPGLRectD& rBox)
{
	if (!IsVisible())
		return;

	// calling base class plot
	CPGLObject::PlotGfx(gfx);
	// Sets dash...
	SetDashGfx(gfx);	
	// set line width
	gfx.SetLineWidth(GetLineWidth());	

	double y = rBox.bottom + rBox.GetHeight()/2.0;
	gfx.DrawLine(rBox.left, y, rBox.right, y);
}
